package org.neuedu.servlet;

import org.neuedu.bean.User;
import org.neuedu.service.UserService;
import org.neuedu.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "UserLogServlet",urlPatterns = "/user/log")
public class UserLogServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String pass = request.getParameter("pass");
        User user = new User();
        user.setEmail(email);
        user.setPassword(pass);
        UserService service = new UserServiceImpl();
        User user1 = service.userLog(user);
        if(user1 == null){
            //没找到，提示用户名或密码错误
            request.setAttribute("logFailMsg","邮箱或密码错误");
            request.getRequestDispatcher("/WEB-INF/jsp/user/log.jsp").forward(request, response);
        }else{
            if(!user1.getEnable()) {
                // 调转首页

                request.getSession().setAttribute("fly_user", user1);

                response.sendRedirect(request.getContextPath() + "/forward/index");
            }else{
                System.out.println("2222222222");
                request.setAttribute("logFailMsg","用户已经被禁用，请联系管理员");
                request.getRequestDispatcher("/WEB-INF/jsp/user/log.jsp").forward(request,response);
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
